//
//  APPerson.m
//  Depressed.domo
//
//  Created by rimi on 16/5/30.
//  Copyright © 2016年 薛涛. All rights reserved.
//

#import "APPerson.h"
#import <objc/runtime.h>
@implementation APPerson
/**解档*/
- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        //定义长度
        unsigned int index = 0;
        //动态获取成员变量数组
        Ivar *ivars = class_copyIvarList([self class], &index);
        for (int i = 0 ; i < index; i++) {
            //取出变量
            Ivar ivar = ivars[i];
            //获取变量名
            const char *name = ivar_getName(ivar);
            //输出属性名字及其在内存偏移量
            NSLog(@"%s %td",name,ivar_getOffset(ivar));
            //解档:解档调用方法decodeObjectForKey
            NSString *key = [NSString stringWithUTF8String:name];
            id value = [aDecoder decodeObjectForKey:key];
            //赋值
            [self setValue:value forKey:key];
        }
        free(ivars);
    }
    return self;
}

/**归档*/
- (void)encodeWithCoder:(NSCoder *)aCoder{
    //定义长度
    unsigned int index = 0;
    //动态获取类成员变量数组
    Ivar *ivars = class_copyIvarList([self class], &index);
    for (int i = 0; i < index; i++) {
        //取出对应的变量
        Ivar ivar = ivars[i];
        //获取取出的变量名
        const char *name = ivar_getName(ivar);
        //归档:归档调用方法encodeObject: forKey:
        NSString *key = [NSString stringWithUTF8String:name];
        id value = [self valueForKey:key];
        [aCoder encodeObject:value forKey:key];
    }
    //释放获取的变量数组
    free(ivars);
}
/**调用时输出内容*/
- (NSString *)debugDescription {
    
    return [NSString stringWithFormat:@"%@ ={name = %@,card = %@,brithday =%@, age =%ld,iphone =%@, adress =%@, height =%f, weigth =%f}",[self class],self.name,self.crad,self.birthday,self.age,self.iphone,self.adress,self.heigth,self.weight];
    
}

/**打印输出内容*/
- (NSString *)description {
    
   return [NSString stringWithFormat:@"%@ ={name = %@,card = %@,brithday =%@, age =%ld,iphone =%@, adress =%@, height =%f, weigth =%f}",[self class],self.name,self.crad,self.birthday,self.age,self.iphone,self.adress,self.heigth,self.weight];

}
@end
